﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace SchoolModel.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure School database creation and initialization Strategy.
            // The initialization strategy is called the first time 
            // the context is used in the application domain. 
            Database.SetInitializer<SchoolEntities>(new SchoolContextInitializer());

            using (var context = new SchoolEntities())
            {
                var department = (from d in context.Departments
                                  where d.Name == "English"
                                  select d).FirstOrDefault();

                Console.WriteLine("Online Courses:");
                foreach (var c in department.Courses.OfType<OnlineCourse>())
                {
                    Console.WriteLine("  " + c.Title);
                    foreach (var i in c.Instructors)
                        Console.WriteLine("Instructor: {0}, {1}", i.FirstName, i.LastName);
                }

                Console.WriteLine();
                // Query for the Physics course using LINQ 
                var physics = (from c in context.Courses
                               where c.Title == "Physics"
                               select c).FirstOrDefault();

                // Use the Query method together with the LINQ’s Count method
                // to get the count of Instructor entities related to this course in the database
                // without actually loading all the instructors.
                var instructorCount = context.Entry(physics)
                    .Collection(c => c.Instructors)
                    .Query()
                    .Count();

                if (instructorCount == 0)
                {
                    Console.WriteLine("Add a new instructor to the {0} course", physics.Title);
                    var physicsInstructor =
                        new Instructor
                            {LastName = "White", FirstName = "Anthony", HireDate = DateTime.Parse("1995-03-11")};

                    // The following line will associate the Instructor with the course
                    // and also add the new instructor to the context.
                    physics.Instructors.Add(physicsInstructor);

                    // Save new entities.
                    int recordsAffected = context.SaveChanges();

                    // Two records were added:
                    // One to the Instructor table and one to the InstructorCourse table.
                    Console.WriteLine(
                        "Saved {0} entities to the database.",
                        recordsAffected);
                }
                else
                    Console.WriteLine("Physics course has an instructor.");

                Console.ReadLine();
            }
        }
    }
}
